package com.zxq.modbus.container;


import com.zxq.modbus.utils.ModBusCrc;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 *  为了获取通道
 */
@Slf4j
public class ModBusChannelContainer {

    /**
     * 容器控制
     */
    static ConcurrentMap<String, ChannelHandlerContext> channelContainer = new ConcurrentHashMap<>();

    /**
     * 容器控制中
     */
    static ConcurrentMap<ChannelHandlerContext,String> keyContainer = new ConcurrentHashMap<>();

    /**
     * 容器控制
     */
    static ConcurrentMap<ChannelHandlerContext, ModBusCrc> modbusContainer = new ConcurrentHashMap<>();

    /**
     * 放置容器
     * @param registerId 地址
     * @param channel 容器
     */
    public static void putChannel(String registerId, ChannelHandlerContext channel) {
        log.info("registerId:" + registerId + " channel:" + channel.toString());
        channelContainer.put(registerId,channel);
        keyContainer.put(channel,registerId);
    }


    /**
     * 溢出容器
     * @param channel 通道
     */
    public static void removeChannel(ChannelHandlerContext channel) {
        //log.info("registerId:" + registerId + " channel:" + channel.toString());
        String  registerId = keyContainer.get(channel);
        channelContainer.remove(registerId);
    }

    /**
     * 获取容器
     * @param registerId 地址
     * @return
     */
    public static ChannelHandlerContext getChannel(String registerId){
       return channelContainer.get(registerId);
    }


    /**
     * 放置modbus 数据
     * @param channel 通道
     * @param modBusCrc 数据
     */
    public static void putModBusCrc(ChannelHandlerContext channel, ModBusCrc modBusCrc) {
        modbusContainer.put(channel,modBusCrc);
    }

    /**
     * 获取modbus数据
     * @param channel 通道
     */
    public static ModBusCrc getModbusCrc(ChannelHandlerContext channel) {
        return  modbusContainer.get(channel);
    }




}
